package com.hualan.service.shangpin;

import com.alibaba.fastjson.JSON;
import com.hualan.entity.Result;
import com.hualan.entity.ResultDate;
import com.hualan.entity.ShangPin;
import com.hualan.utils.EntityFactory;
import com.hualan.utils.ExecUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@WebServlet(urlPatterns = {"/panghudaojiaBackend/shangpin/shangpinList","/panghudaojiaBackend/shangpin/modifyShangPin"})
public class ShangPinService extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String location = req.getServletPath();
        if(location.equals("/panghudaojiaBackend/shangpin/modifyShangPin")) {
            int goodsStatus = Integer.parseInt(req.getParameter("goodsStatus"));
            int goodsId = Integer.parseInt(req.getParameter("goodsId"));
            //修改商品状态
            String sql = "UPDATE c_shangpin SET goodsStatus = ? where goodsId = ?";
            List<Object> list = new ArrayList<>();
            list.add(goodsStatus);
            list.add(goodsId);
            ExecUtil.execute(sql,list,req,true);
            ExecUtil.commit();
            ExecUtil.close();
            resp.getWriter().write(JSON.toJSONString(new Result(200, "修改成功！")));
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String location = req.getServletPath();
        if(location.equals("/panghudaojiaBackend/shangpin/shangpinList")){
            String sql = "select * from c_shangpin where ";
            List<Object> list = new ArrayList<>();
            if(!Objects.equals(req.getParameter("name"), "")){
                sql += "goodsName like ? and ";
                list.add("%"+req.getParameter("name")+"%");
            }
            if(!Objects.equals(req.getParameter("startTime"), "") && !Objects.equals(req.getParameter("endTime"), "")){
                sql += "addTime between ? and ? and ";
                list.add(req.getParameter("startTime"));
                list.add(req.getParameter("endTime"));
            }
            sql+="\'a\'=\'a\' ";
            Integer pageNum = Integer.parseInt(req.getParameter("pageNum"));
            Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
            Integer start = pageSize*(pageNum-1);
            Integer end = pageSize*pageNum;
            sql += "limit "+start+","+end;

            ResultSet resultSet =(ResultSet) ExecUtil.execute(sql,list,req,false);
            ExecUtil.commit();
            List<Object> goods = new ArrayList<>();
            try{
                while (resultSet.next()){
                    EntityFactory entityFactory = new EntityFactory(ShangPin.class);
                    ShangPin shangPin =(ShangPin) entityFactory.startCombine(resultSet);
                    goods.add(shangPin);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            ExecUtil.close();
            sql = "select count(goodsId) total from c_shangpin";
            list.clear();
            ResultSet resultSet1 = (ResultSet) ExecUtil.execute(sql,list,req,false);
            ExecUtil.commit();
            int total = 0;
            try {
                while (resultSet1.next()){
                    total = resultSet1.getInt(1);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            ExecUtil.close();
            resp.getWriter().write(JSON.toJSONString(new Result(200,new ResultDate(pageNum,pageSize,total,total/pageSize+(total%pageSize!=0?1:0),goods))));
        }
    }
}
